<template>
  <div class="pic">
    <Header></Header>
    <div class="pic-bg"></div>
    <div class="pic-main">
      <div class="pic-type">
        <ul>
          <li>最新</li>
          <li>游记</li>
          <li>日常</li>
          <li>游戏</li>
          <li>美食</li>
        </ul>
      </div>
      <div class="all-pic">
        <div class="pic-item" v-for="(item, index) in pics" :key="index">
          <img
            :src="'http://47.113.189.57:5000/pic/' + item.paddress"
            alt="图片"
            @click.self="showBigImage($event)"
            class="mouseCursorLink"
          />
          <p>摄于{{ item.ptime }}</p>
          <span>{{ item.ptitle }}</span>
          <div>
            <i class="iconfont icon-fasongx"></i
            ><span
              ><i
                class="HeartAnimation mouseCursorLink"
                @click="picLike(item.picid, index)"
              ></i
              >{{ item.plike }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <ShowBigImg
      :visible="photoVisible"
      :url="bigImgUrl"
      @closeClick="
        () => {
          photoVisible = false
        }
      "
    ></ShowBigImg>
    <Footer></Footer>
  </div>
</template>

<script>
// @ is an alias to /src
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import ShowBigImg from '@/components/ShowBigImg.vue'
import $ from 'jquery'

export default {
  name: 'PicView',
  components: {
    Header,
    Footer,
    ShowBigImg,
  },
  data() {
    return {
      pics: [],
      photoVisible: false,
      bigImgUrl: '',
    }
  },
  methods: {
    getPics() {
      this.$api.pic
        .getPics()
        .then((res) => {
          // console.log(res)
          this.pics = res.data.data
        })
        .catch((err) => {
          console.log(err)
          this.$message({
            message: '网页发生了错误！',
            type: 'error',
          })
        })
    },
    showBigImage(e) {
      //点击图片函数，点击后，把photoVisible设置成true
      if (e != '') {
        this.photoVisible = true
        this.bigImgUrl = e.currentTarget.src
      }
    },
    picLike(id, index) {
      let $dz = $('i.HeartAnimation').eq(index)
      if ($dz.hasClass('like-active')) {
        var i = -1
      } else {
        var i = 1
      }

      this.pics[index].plike = parseInt(this.pics[index].plike) + i
      console.log(this.pics[index])
      const params = {
        id: id,
        like: this.pics[index].plike,
      }
      this.$api.pic
        .picLike(params)
        .then((res) => {
          if (res.data.code == 200) {
            if (i == 1) {
              this.$message.success('点赞成功！')
            } else {
              this.$message.success('取消点赞成功！')
            }
          } else {
            this.$message.err('???怎么不能点赞啊')
          }
        })
        .catch((err) => {
          console.log(err)
          this.$message({
            message: '???网页怎么出问题了',
            type: 'error',
          })
        })
      $dz.toggleClass('like-active')
    },
  },
  created() {
    this.getPics()
  },
}
</script>

<style lang="scss" scoped>
.pic {
  .pic-bg {
    position: fixed;
    top: 0;
    left: 0;
    background: url('../assets/img/mood/1.jpg') center no-repeat;
    background-size: cover;
    height: 100%;
    width: 100%;
    z-index: -10;
  }
  .pic-main {
    max-width: 80em;
    width: 100%;
    padding: 20px 20px;
    margin: 120px auto;
    min-height: 600px;
    > .pic-type {
      > ul {
        display: flex;
        justify-content: center;
        > li {
          padding: 0.45em 1em;
          border-radius: 2em;
          transition: color 0.3s, background-color 0.3s, transform 0.3s;
          background-color: white;
          margin-right: 1em;
          cursor: pointer;
          &:hover {
            background-color: orange;
            color: white;
          }
        }
      }
    }
    > .all-pic {
      padding: 60px 0;
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
      > .pic-item {
        width: calc((100% - 60px) / 3);
        height: 13em;
        margin-right: 30px;
        position: relative;
        overflow: hidden;
        cursor: pointer;
        border-radius: 10px;
        margin-bottom: 1.5em;
        > img {
          width: 100%;
          height: 100%;
        }
        > p {
          position: absolute;
          //   height: 100%;
          //   width: 100%;
          //   background-color: rgba(0, 0, 0, 0.4);
          top: 0;
          left: 0;
          color: white;
          padding: 20px;
          font-size: 1.5em;
          opacity: 0;
          transition: opacity 0.3s;
        }
        > span {
          display: block;
          background-color: rgba($color: #000000, $alpha: 0.3);
          width: 100%;
          text-align: center;
          line-height: 2em;
          font-size: 1.1em;
          position: absolute;
          left: 50%;
          bottom: 0;
          transform: translate(-50%, 0);
          color: white;
          transition: transform 0.3s;
        }
        > div {
          position: absolute;
          display: flex;
          justify-content: space-between;
          bottom: 0;
          left: 0;
          color: white;
          width: 100%;
          padding: 10px 20px;
          i {
            margin-right: 0.5em;
          }
          span > i.HeartAnimation {
            left: auto;
            right: 0%;
          }
        }
        &:hover p {
          opacity: 1;
        }
        &:hover > span {
          transform: translate(-50%, 100%);
        }
        &:nth-child(3n) {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
